extends Node2D

const wordsArray = ["apple", "book", "cat", "dog", "elephant", "flower", "giraffe", "hat", "ice", "juice", "kite", "lion", "moon", "noodle", "orange", "pen", "queen", "rabbit", "sun", "tree", "umbrella", "violin", "water", "xylophone", "yacht", "zebra"];
var to_spell = []
var idx = 0
var errors = 0
var spelled = []
var spells = []

func _ready():
	randomize()
	to_spell = []
	for i in 3:
		var r = int(rand_range(0,len(wordsArray)))
		to_spell.append(wordsArray[r])
	print(to_spell)
	$Label.text = to_spell[0]
	$Label2.text = to_spell[1]
	$Label3.text = to_spell[2]
	pass

func _input(event):
	if event is InputEventKey and event.is_pressed():
		var code = OS.get_scancode_string(event.scancode).to_lower()
		var scancode = event.scancode
		if code in to_spell[idx]:
			spelled.append(code)
			print(spelled)
			var tmp = ""
			for i in spelled:
				tmp += i
			$entered.text = tmp
		else:
			errors += 1
			$error.text = "错误:" + str(errors)
			
		if scancode == KEY_ENTER:
			var entered_string = ""
			for string in spelled:
				entered_string += string
			print(entered_string)
			if entered_string == to_spell[idx]:
				idx += 1
				spelled = []
				if idx == 1:
					$Sprite.position = $Label.rect_position
				elif idx == 2:
					$Sprite.position = $Label2.rect_position
				elif idx == 3:
					$Sprite.position = $Label3.rect_position
					yield(get_tree().create_timer(0.25),"timeout")
					$Sprite.position = $final.rect_position
				print(idx)
		elif scancode == KEY_BACKSPACE:
			spelled.pop_back()
			$entered.text = String(spelled)
	pass


func _on_replay_pressed():
	get_tree().reload_current_scene()
	pass # Replace with function body.
